import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestClient03 {
    public static void main(String[] args) throws Exception {
        // 1. 定义Scanner类和要录入的字符串
        Scanner sc = new Scanner(System.in);
        List<Teacher> teachers = new ArrayList<>();

        // 2. 逐行录入老师信息，直到遇到quit结束录入
        System.out.println("请输入老师信息，格式为：姓名-年龄-工资");
        while (true) {
            String input = sc.nextLine();
            if ("quit".equals(input)) {
                break;
            }

            // 3.1 拆分录入的字符串
            String[] parts = input.split("-");
            String name = parts[0];
            int age = Integer.parseInt(parts[1]);
            double salary = Double.parseDouble(parts[2]);

            // 3.2 创建Teacher对象，并为对应属性赋值
            Teacher teacher = new Teacher(name, age, salary);

            // 3.3 将老师对象添加到集合中
            teachers.add(teacher);
        }

        // 4. 搭建TCP客户端
        Socket socket = new Socket("127.0.0.1", 8002);
        System.out.println("客户端成功启动：" + socket);

        // 5. 发送集合对象
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(teachers);
        oos.flush();
        System.out.println("发送集合成功！");

        // 6. 从服务器接收数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String response = br.readLine();
        System.out.println("客户端收到服务器信息：" + response);

        // 7. 关闭资源
        oos.close();
        br.close();
        socket.close();
        System.out.println("客户端即将关闭");
    }
}